package com.nbooo.ui
{
	import com.somewater.storage.Lib;
	import com.somewater.storage.RoomDef;
	
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	import flash.text.TextField;
	import flash.utils.Timer;

	public class RoomIcon extends Sprite
	{
		public var roomName:String;
		public var minXp:int;
		public var period:int;
		public var players:int;
		public var playersMax:int;
		public var roomDef:RoomDef;
		public var startTime:Number;
		private var content:Sprite;
		private var updateTimer:Timer;
		
		public function RoomIcon()
		{
			super();
			content = Lib.getMc("GameListElement");
			addChild(content);
			this.useHandCursor = true;
			this.buttonMode = true;
		}
		public function initialize(_roomInfo:RoomDef):void
		{
			roomDef = _roomInfo;
			
			this.roomName = _roomInfo.name;
			this.minXp = _roomInfo.min_xp;
			this.period = _roomInfo.period;
			this.startTime = _roomInfo.start;
			//this.players = _roomInfo.players;
			
			
			
			(content.getChildByName("tf_roomName")as TextField).text = this.roomName;
			(content.getChildByName("tf_people")as TextField).text = this.players.toString() + " из " + this.playersMax;
			update(null);
			//updateTimer = new Timer(1000);
			//updateTimer.addEventListener(TimerEvent.TIMER, update);
			//updateTimer.start();
			
		}
		private function update(e:TimerEvent):void
		{	
		 	var date:Date = new Date();
			var currTime:Number = date.time;
			var timeLeft:Number = startTime - currTime;
			var hours:Number = Math.floor(timeLeft / 1000 / 60 / 60);
			var minutes:Number = Math.floor((timeLeft - (hours * 1000 * 60 * 60)) / 1000 / 60);
			var seconds:Number = Math.floor((timeLeft -  (hours * 1000 * 60 * 60) - (minutes* 1000 * 60)) / 1000);
			var sMinutes:String = minutes.toString();
			var sSeconds:String = seconds.toString();
			
			if(minutes < 10)
			{
				sMinutes ="0" + minutes.toString();
			}
			if(seconds < 10)
			{
				sSeconds = "0" + seconds.toString();
			}
			(content.getChildByName("tf_time")as TextField).text = sMinutes + ":" + sSeconds;
			
		
			
			
		}
	}
}